package com.dubbo.cp.consumer.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dubbo.cp.IService.ClassroomService;
import com.dubbo.cp.entity.ClassroomEntity;

import com.dubbo.cp.uitls.Pager;

@Controller
@RequestMapping("/classroom")
public class ClassroomController extends BaseController{

	@Reference
	private ClassroomService classroomServiceImpl;
	private Pager<ClassroomEntity> pager;

	@RequestMapping(value="uList")
	public String classroomList(Model model) {

		List<ClassroomEntity> useList = classroomServiceImpl.getClassroomList();
		model.addAttribute("useList", useList);

		return "classroomList";

	}

	@RequestMapping(value="findClassroomByPage")
	public ModelAndView findByPage(Model model,String currentPage) {

		System.out.println("进入分页查询");
		int pageSize = Integer.parseInt(currentPage);
		if(pager==null) {
			pager = new Pager<ClassroomEntity>();
		}
		pager.setCurrentPage(pageSize);
		int length = pager.getCurrentPage();
		int recordSize = pager.getRecordSize();
		int start = (length-1)*recordSize;
		pager.setList(classroomServiceImpl.getClassroomList());
		pager.initTotalPage();
		List<ClassroomEntity>ulist = classroomServiceImpl.getClassroomByPage(start, pager.getRecordSize());
		model.addAttribute("ulist", ulist);
		model.addAttribute("pager", pager);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/classroomList");
		return modelAndView;


	}

	/**
	 * 
	 * 
	 * 添加班级跳转
	 * @return
	 */
	@RequestMapping(value="inputClassroom")
	public String inputClassroom() {
		return "classroomAdd";

	}

	public String insertClassroom(@Valid ClassroomEntity classroomEntity) {
		System.out.println("添加");
		classroomServiceImpl.insertClassroomEntity(classroomEntity);
		return "redirect:findClassroomByPage?currentPage=1";

	}
	
	 /**
     * 编辑跳转
     * @return
     */
	
	@RequestMapping(value="editClassroom")
	public ModelAndView editClassroom(Model model,@RequestParam int id) {
		
		System.out.println("id===="+id);
		
		ClassroomEntity classroomEntity = classroomServiceImpl.findClassroomByClassroomid(id);
		model.addAttribute("classroomEntity", classroomEntity);
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/classroomEdit");
		return modelAndView;
	}
	@PostMapping(value="updateClassroom")
	public String updateClassroom(ClassroomEntity classroomEntity) {
		classroomServiceImpl.updateClassroomEntity(classroomEntity);
		
		return "redirect:findClassroomByPage?currentPage=1";
		
	}
	
	@RequestMapping(value="deleteClassroom")
	public String deleteClassroom(@RequestParam int id) {
		System.out.println(id);
		classroomServiceImpl.deleteClassroomEntity(id);
		return "redirect:findClassroomByPage?currentPage=1";
		
	}
	
}
